#!/usr/bin/perl -w
#
# $Id: ClistWindow.pm,v 1.11 2003/07/10 16:51:43 solovam Exp $
#
# This file is a part of gkismet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

#
# ClistWindow class
#
package ClistWindow;

use Gtk;
use Misc;
use BssConnObserver;
use strict;
@ClistWindow::ISA = qw(BssConnObserver);

#
# Constructor
#
sub new
{
	my $type = shift;
	my $self = new BssConnObserver(@_);
	bless $self, $type;

	# Main window
	my $window = new Gtk::Window('dialog');
	$window->signal_connect("delete_event", sub {$self->deleteHandler()});
	$window->set_default_size(Gtk::Gdk->screen_width() * 0.8, Gtk::Gdk->screen_height() * 0.5);
	$window->set_title($self->getWindowName() . ' ' . $self->{'connection'}->getNetwork()->{$self->{'bssid'}}{'ssid'} . ' ' . $self->{'bssid'});
	$self->{'window'} = $window;

	# Scrolled window
	my $scrolledWindow = new Gtk::ScrolledWindow(undef, undef);

	# Clist
	my @titles = $self->getColumnTitles();
	my $clist = new_with_titles Gtk::CList(@titles);
	for(my $i = 0; $i <= $#titles; $i++)
	{
		$clist->set_column_auto_resize($i, $true);
	}
	$self->{'clist'} = $clist;
	$self->{'clist'}->set_selectable($false);
	$self->{'nRows'} = 0;

	# Buttonbox, buttons
	my $bbox = new Gtk::HButtonBox();
	$bbox->set_layout('spread');
	$bbox->set_spacing(5);
	my $buttonClose = new Gtk::Button("Close");
	$buttonClose->signal_connect( "clicked", sub {$self->closeClicked()});
	$bbox->add($buttonClose);
	my $buttonPause = new Gtk::Button("Pause");
	$buttonPause->signal_connect( "clicked", sub {$self->pauseClicked($buttonPause)});
	$bbox->add($buttonPause);

	# Assemble all
	$scrolledWindow->add($clist);
	$self->{'scrolledWindow'} = $scrolledWindow;
	my $vbox = new Gtk::VBox($false, 0);
	$vbox->pack_start($scrolledWindow, $true, $true, 0);
	$vbox->pack_end($bbox, $false, $false, 10);
	$window->add($vbox);
	$window->show_all();

	# Misc
	$self->{'paused'} = $false;

	# Activate connection
	$self->activateConnection();
	$self->{'connection'}->addObserver($self);

	return $self;
}

#
# Close window
#
sub close
{
	my $self = shift;
	$self->deactivateConnection();
	$self->SUPER::close();
}

#
# Pause button clicked
#
sub pauseClicked
{
	my $self = shift;
	my $button = shift;
	if($self->{'paused'} == $false)
	{
		$self->{'paused'} = $true;
		$button->child()->set("Resume");
	}
	else
	{
		$self->{'paused'} = $false;
		my $label = new Gtk::Label('Pause');
		$button->child()->set("Pause");
	}
	return($true);
}

#
# Get widget
#
sub getWidget
{ 
	my $self = shift;
	return $self->{'window'};
}

#
# Handle an update from an observable
#
sub update
{
	my $self = shift;
	my $object = shift;
	my $data = shift;

	if($object->isa('Connection') && $self->{'connection'} eq $object)
	{
		if($self->isInterstingUpdate($data) && $self->{'paused'} == $false)
		{
			my @columns = $self->getColumnData();

			$self->{'clist'}->freeze();

			# Save scrolling settings
			my $adjustment = $self->{'scrolledWindow'}->get_vadjustment();
                        my $value = $adjustment->get_value();
                        my $page_size = $adjustment->page_size();
                        my $step_increment = $adjustment->step_increment();
                        my $upper = $adjustment->upper();

			$self->{'clist'}->append(@columns);
			$self->{'nRows'}++;
			# Keep old row in focus (wow! what a messy business!)
                        $adjustment->set_value($value);

			if($self->{'nRows'} > $self->getWindowDepth())
			{
				$self->{'clist'}->remove(0);
				$self->{'nRows'}--;
				# Still keep old row in focus
				$adjustment->set_value($value - $step_increment - 1);
			}

			# If we are scrolled to the bottom, advance to the end
			if($value + $page_size >= $upper)
			{
				$adjustment->set_value($value + $step_increment + 1);
			}
			# Take care of starting being scrolled to the bottom
			elsif($value == 0 && $page_size + $step_increment >= $upper)
			{
				$adjustment->set_value($upper);
			}

			$adjustment->value_changed();

			$self->{'clist'}->thaw();

		}
	}
}

1;
